package com.bs.sc.front.goodsCar.service.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.front.goodsCar.dao.GoodsCarDao;
import com.bs.sc.front.goodsCar.dao.GoodsCarItemDao;
import com.bs.sc.front.goodsCar.entity.GoodsCar;
import com.bs.sc.front.goodsCar.entity.GoodsCarItem;
import com.bs.sc.front.goodsCar.service.GoodsCarService;
import com.bs.sc.front.user.entity.User;
import com.bs.sc.mgr.goods.dao.GoodsDao;
import com.bs.sc.mgr.goods.entity.Goods;

@Service("goodsCarService")
@Transactional
public class GoodsCarServiceImpl implements GoodsCarService {

	@Autowired
	private GoodsCarDao goodsCarDao;
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private GoodsCarItemDao goodsCarItemDao;

	@Override
	public AjaxJson addGoodsCar(HttpServletRequest request, String goodsId) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("添加购物车失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			Optional<Goods> op = goodsDao.findById(goodsId);
			Goods goods = null;
			if (op != null) {
				goods = op.get();
			} else {
				j.setMsg("数据异常");
				return j;
			}
			List<Map<String, Object>> carList = goodsCarDao.findByUserIdAndGoodsId(user.getId(), goods.getId());
			if (carList.size() > 0) {
				for (Map<String, Object> map : carList) {
					String itemId = (String) map.get("itemId");
					System.out.println("goods car item id is:" + itemId);
					Optional<GoodsCarItem> optional = goodsCarItemDao.findById(itemId);
					if (optional.isPresent()) {
						GoodsCarItem item = optional.get();
						item.setCount(item.getCount() + 1);
						goodsCarItemDao.save(item);
					}
				}
				j.setCode("success");
				j.setMsg("加入购物车成功");
				return j;
			}
			GoodsCar car = new GoodsCar();
			car.setUser(user);
			goodsCarDao.save(car);
			GoodsCarItem item = new GoodsCarItem();
			item.setCount(1);
			item.setGoods(goods);
			item.setGoodsCar(car);
			goodsCarItemDao.save(item);
			j.setCode("success");
			j.setMsg("加入购物车成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson getCarGoods(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取购物车数据失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			List<Map<String, Object>> cars = goodsCarDao.findByUserId(user.getId());
			List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
			for (Map<String, Object> m : cars) {
				Map<String, Object> temp = new HashMap<String, Object>();
				temp.put("count", m.get("count"));
				temp.put("itemId", m.get("itemId"));
				temp.put("carId", m.get("carId"));
				Optional<Goods> goods = goodsDao.findById((String) m.get("goodsId"));
				temp.put("goods", goods.get());
				result.add(temp);
			}
			j.setCode("success");
			j.setMsg("获取购物车数据成功");
			j.setData(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson goodsCarService(HttpServletRequest request, String goodsId, int count) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("修改购物车商品数量失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			List<Map<String, Object>> carList = goodsCarDao.findByUserIdAndGoodsId(user.getId(), goodsId);
			if (carList.size() > 0) {
				for (Map<String, Object> map : carList) {
					String itemId = (String) map.get("itemId");
					System.out.println("change goods number car item id is:" + itemId);
					Optional<GoodsCarItem> optional = goodsCarItemDao.findById(itemId);
					if (optional.isPresent()) {
						GoodsCarItem item = optional.get();
						item.setCount(count);
						goodsCarItemDao.save(item);
					}
				}
				j.setCode("success");
				j.setMsg("修改购物车商品数量成功");
				return j;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson deleteCarItem(String itemId, String carId) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("删除购物车商品失败");
		try {
			goodsCarDao.deleteById(carId);
			goodsCarItemDao.deleteById(itemId);
			j.setCode("success");
			j.setMsg("删除购物车商品成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}
}
